package com.jhgsys.internal.sypq.controller;

//import com.jhgsys.internal.base.entity.BaseAreaScheduling;
import com.jhgsys.internal.common.annotation.Log;
import com.jhgsys.internal.common.constant.FileUploadConstant;
import com.jhgsys.internal.common.controller.BaseController;
import com.jhgsys.internal.common.controller.FileUploader;
import com.jhgsys.internal.common.entity.*;
import com.jhgsys.internal.common.exception.FebsException;
import com.jhgsys.internal.common.utils.Plupload;
import com.jhgsys.internal.sypq.entity.*;
import com.jhgsys.internal.sypq.service.ICustomerService;
import com.jhgsys.internal.sypq.service.ISypqProjectService;
import com.jhgsys.internal.system.entity.User;
import com.jhgsys.internal.system.service.IUserService;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * 项目设置 Controller
 *
 * @author ganjun
 * @date 2024-11-12 10:58:48
 */
@Slf4j
@Validated
@Controller
public class SypqProjectController extends BaseController {

    @Autowired
    private ISypqProjectService sypqProjectService;
    @Autowired
    private IUserService userService;
    @Autowired
    private ICustomerService customerService;



    @GetMapping(FebsConstant.VIEW_PREFIX + "sypqProject")
    @RequiresPermissions("sypqProject:view")
    public String sypqProjectIndex(Model model) {
        boolean isAdmin = isAdmin();
        User user = (User) getSubject().getPrincipal();
        model.addAttribute("userid", user.getUserId());
        model.addAttribute("isAdmin", isAdmin);
        return jumpPage("sypq/sypqProject/sypqProjectList");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "sypqProject/add")
    @RequiresPermissions("sypqProject:add")
    public String sypqProjectAdd(Model model) {
        SypqProject sypqProject = new SypqProject();
        String projectcode = sypqProjectService.getNewProjectCode();
        sypqProject.setProjectCode(projectcode);
        model.addAttribute("sypqProject", sypqProject);
        model.addAttribute("userList", userService.getUserList());
        Customer  customer=new Customer();
        customer.setStatus(1);
        model.addAttribute("customerList", customerService.findCustomerList(customer));
        model.addAttribute("type", "add");
        model.addAttribute("oldProjectCode", "");
        return jumpPage("sypq/sypqProject/sypqProjectEdit");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "sypqProject/update/{id}")
    @RequiresPermissions("sypqProject:edit")
    public String sypqProjectUpdate(Model model, @PathVariable("id") String id) {
        SypqProjectDto sypqProjectDto = new SypqProjectDto();
        sypqProjectDto.setProjectCode(id);
        List<SypqProjectDto> list = sypqProjectService.findSypqProjectList(sypqProjectDto,"");
        SypqProjectDto sypqProject = new SypqProjectDto();
        if (list.size() > 0) {
            sypqProject = list.get(0);
        }
        model.addAttribute("sypqProject", sypqProject);
        model.addAttribute("userList", userService.getUserList());
        Customer  customer=new Customer();
        customer.setStatus(1);
        model.addAttribute("customerList", customerService.findCustomerList(customer));
        model.addAttribute("type", "edit");
        model.addAttribute("oldProjectCode", "");
        return jumpPage("sypq/sypqProject/sypqProjectEdit");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "sypqProject/copy/{id}")
    @RequiresPermissions("sypqProject:add")
    public String sypqProjectCopy(Model model, @PathVariable("id") String id) {
        SypqProjectDto sypqProjectDto = new SypqProjectDto();
        sypqProjectDto.setProjectCode(id);
        List<SypqProjectDto> list = sypqProjectService.findSypqProjectList(sypqProjectDto,"");
        SypqProjectDto sypqProject = new SypqProjectDto();
        if (list.size() > 0) {
            sypqProject = list.get(0);
        }
        sypqProject.setCreatedTime(null);
        String projectcode = sypqProjectService.getNewProjectCode();
        sypqProject.setProjectCode(projectcode);
        sypqProject.setApproveStatus(1);
        //拷贝船模.桨模。附体信息
    //    sypqProjectService.copymodel(id,projectcode);
        model.addAttribute("sypqProject", sypqProject);
        model.addAttribute("userList", userService.getUserList());
        model.addAttribute("customerList", customerService.findCustomerList(new Customer()));
        model.addAttribute("type", "copy");
        model.addAttribute("oldProjectCode", id);
        return jumpPage("sypq/sypqProject/sypqProjectEdit");
    }

    //查看详情
    @GetMapping(FebsConstant.VIEW_PREFIX + "sypqProject/detail/{id}")
    @RequiresPermissions("sypqProject:detail")
    public String sypqProjectDetail(Model model, @PathVariable("id") String id) {
        SypqProjectDto sypqProjectDto = new SypqProjectDto();
        sypqProjectDto.setProjectCode(id);
        List<SypqProjectDto> list = sypqProjectService.findSypqProjectList(sypqProjectDto,"");
        SypqProjectDto sypqProject = new SypqProjectDto();
        if (list.size() > 0) {
            sypqProject = list.get(0);
        }
        model.addAttribute("sypqProject", sypqProject);
        model.addAttribute("type", "detail");
        return jumpPage("sypq/sypqProject/sypqProjectDetail");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "sypqProject/audit/{id}")
    @RequiresPermissions("sypqProject:audit")
    public String sypqProjectAudit(Model model, @PathVariable("id") String id) {
        SypqProjectDto sypqProjectDto = new SypqProjectDto();
        sypqProjectDto.setProjectCode(id);
        List<SypqProjectDto> list = sypqProjectService.findSypqProjectList(sypqProjectDto,"");
        SypqProjectDto sypqProject = new SypqProjectDto();
        if (list.size() > 0) {
            sypqProject = list.get(0);
        }
        model.addAttribute("sypqProject", sypqProject);
        model.addAttribute("type", "audit");
        return jumpPage("sypq/sypqProject/sypqProjectDetail");
    }

    /**
     * 查询（所有）
     *
     * @author ganjun
     * @date 2024-11-12 10:58:48
     */
    @GetMapping("sypqProject")
    @ResponseBody
    @RequiresPermissions("sypqProject:view")
    public FebsResponse getAllSypqProjects(SypqProject sypqProject) {
        try {
            return success(sypqProjectService.findSypqProjectList(sypqProject));
        } catch (Exception ex) {
            return failure(ex);
        }
    }

    /**
     * 查询（分页）
     *
     * @author ganjun
     * @date 2024-11-12 10:58:48
     */
    @GetMapping("sypqProject/list")
    @ResponseBody
    @RequiresPermissions("sypqProject:view")
    public FebsResponse sypqProjectList(QueryRequest request, Model model, SypqProjectDto sypqProjectDto) {
        try {
            String userid=getCurrentUser().getUserId();
            Boolean isAdmin=isAdmin();
            if(isAdmin){
                userid="";
            }
            Map<String, Object> dataTable = getDataTable(this.sypqProjectService.findSypqProjectPage(request, sypqProjectDto,userid));
            return success(dataTable);
        } catch (Exception ex) {
            return failure(ex);
        }
    }

    @GetMapping("sypqProject/excel")
    @ResponseBody
    @RequiresPermissions("sypqProject:view")
    public void export(SypqProjectDto sypqProjectDto, HttpServletResponse response) throws FebsException {
        try {
            List<SypqProjectDto> list = this.sypqProjectService.findSypqProjectList(sypqProjectDto,"");
            ExcelKit.$Export(SypqProjectExcelDto.class, response).downXlsx(list, true);
        } catch (Exception e) {
            String message = "导出Excel失败";
            e.printStackTrace();
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    @Log("新增SypqProject")
    @PostMapping("sypqProject/")
    @ResponseBody
    public FebsResponse addSypqProject(@RequestBody SypqProjectDtoParam sypqProjectDto)  throws FebsException {
        try {
            this.sypqProjectService.createSypqProject(sypqProjectDto);
            return success();
        } catch (Exception ex) {
            return failure(ex, "新增失败");
        }
    }

    /**
     * 修改 SypqProject
     *
     * @author ganjun
     * @date 2024-11-12 10:58:48
     */
    @Log("修改SypqProject")
    @PostMapping(value = "sypqProject/update/")
    @ResponseBody
    @RequiresPermissions("sypqProject:edit")
    public FebsResponse updateSypqProject(@RequestBody SypqProjectDtoParam dto) throws FebsException {
        try {
            this.sypqProjectService.updateSypqProject(dto);
            return success();
        } catch (Exception ex) {
            return failure(ex, "修改失败");
        }
    }


    @Log("提交审批")
    @PostMapping(value = "sypqProject/commit/")
    @ResponseBody
  //  @RequiresPermissions("sypqProject:commit")
    public FebsResponse commitSypqProject(@RequestBody SypqProjectDtoParam dto) throws FebsException {
        try {
            this.sypqProjectService.commitSypqProject(dto);
            return success();
        } catch (Exception ex) {
            return failure(ex, "修改失败");
        }
    }


    @Log("保存审批意见")
    @PostMapping(value = "sypqProject/saveAudit")
    @ResponseBody
    //  @RequiresPermissions("sypqProject:saveAudit")
    public FebsResponse saveAuditSypqProject(@RequestBody WorkflowRecord workflowRecord) throws FebsException {
        try {
            this.sypqProjectService.saveAuditSypqProject(workflowRecord);
            return success();
        } catch (Exception ex) {
            return failure(ex, "修改失败");
        }
    }

    /**
     * 逻辑删除 SypqProject
     *
     * @author ganjun
     * @date 2024-11-12 10:58:48
     */
    @Log("逻辑删除SypqProject")
    @GetMapping("sypqProject/delete/{id}")
    @ResponseBody
    @RequiresPermissions("sypqProject:delete")
    public FebsResponse deleteSypqProject(@PathVariable("id") String sypqProjectIds) throws FebsException {
        try {
            SypqProject sypqProject = new SypqProject();
            this.sypqProjectService.deleteSypqProject(sypqProject, sypqProjectIds);
            return success();
        } catch (Exception ex) {
            return failure(ex, "删除失败");
        }
    }

    @Log("逻辑删除SypqProject")
    @GetMapping("sypqProject/stop/{id}")
    @ResponseBody
    @RequiresPermissions("sypqProject:edit")
    public FebsResponse  stopSypqProject(@PathVariable("id") String id) throws FebsException {
        try {
            this.sypqProjectService.stopSypqProject(id);
            return success();
        } catch (Exception ex) {
            return failure(ex, "删除失败");
        }
    }


    @Log("检查项目名称或编码是否存在")
    @GetMapping("sypqProject/check")
    @ResponseBody
    public FebsResponse   checkProject(SypqProject sypqProject) throws FebsException {
         String result=   this.sypqProjectService.checkProject(sypqProject);
        return   new FebsResponse().success().data(result);
    }
    /**
     * 物理删除 SypqProject
     *
     * @author ganjun
     * @date 2024-11-12 10:58:48
     */
    @Log("物理删除SypqProject")
    @GetMapping("sypqProject/physicallyDelete/{id}")
    @ResponseBody
    @RequiresPermissions("sypqProject:delete")
    public FebsResponse physicallyDeleteSypqProject(@PathVariable("id") String sypqProjectIds) throws FebsException {
        try {
            this.sypqProjectService.physicallyDeleteSypqProject(sypqProjectIds);
            return success();
        } catch (Exception ex) {
            return failure(ex, "删除失败");
        }
    }


    @GetMapping("sypqProject/getUserList")
    @ResponseBody
    public FebsResponse getUserList() throws FebsException {
        try {
            return new FebsResponse().success().data(userService.getUserList());
        } catch (Exception e) {
            String message = "修改角色失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }



    @RequestMapping(value = "sypqProject/upload", method = RequestMethod.POST)
    @ResponseBody
    public FebsResponse uploadImage(Plupload plupload, HttpServletRequest request) {
        if (!FileUploader.checkFileType(plupload.getName(), FileUploader.allowImageFiles)
            &&!FileUploader.checkFileType(plupload.getName(),FileUploader. allowVideoFiles)
            &&!FileUploader.checkFileType(plupload.getName(), FileUploader.allowOtherFiles)) {
            return new FebsResponse().fail().message(FileUploader.MSG_ERROR_FILE_TYPE);
        }
        plupload.setFilePath(FileUploadConstant.BASE_FILE_PATH);
        FebsResponse objects = FileUploader. fileUpCommon(plupload, request, plupload.getFilePath());
        return objects;
    }
}
